package com.jason.java.lib.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description: ServerSocket
 * @author: ChenBaoYang
 * @createDate: 2022/3/18 10:04 上午
 */
public class MyServerSocket {

    //线程工厂
    private static ThreadFactory threadFactory = new ThreadFactory() {
        private final AtomicInteger integer = new AtomicInteger();

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "myThreadPool thread:" + integer.getAndIncrement());
        }
    };

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5),threadFactory);
        try {
            //ServerSocket创建TCP服务器端, 监听端口是8881
            ServerSocket serverSocket = new ServerSocket(8888);
            //获取本地的网络地址数据
            InetAddress address = InetAddress.getLocalHost();
            //获取本地ip
            String ip = address.getHostAddress();
            System.out.println("服务端IP地址："+ip);
            //调用accept()方法监听客户端的请求
            while(true){
                Socket socket = serverSocket.accept();
                executor.execute(new ServerListener(socket));
                executor.execute(new SeverSend(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    static class ServerListener implements Runnable{

        private Socket socket;

        public ServerListener(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            InputStream is = null;
            try {
                is = socket.getInputStream();
                //转埕InputStreamReader
                InputStreamReader isr = new InputStreamReader(is,"UTF-8");
                //专程BufferReader
                BufferedReader reader = new BufferedReader(isr);
                //获取数据转成字符串
                String info = null;
                while ((info = reader.readLine()) != null) {
                    //循环读取客户端的信息
                    System.out.println("客户端发送过来的信息:" + info);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static class SeverSend implements Runnable{

        private final Socket socket;

        public SeverSend(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);
                // 获取客户端的ip地址
                InetAddress inetAddress = InetAddress.getLocalHost();
                String ip = inetAddress.getHostAddress();
                while(true){
                    Thread.sleep(1000L);
                    // 输出内容
                    pw.write("客户端：~" + ip + "~ 接入服务器！！");
                    pw.flush();
                    pw.close();
                }

            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
